# vim:ts=4:sw=4:noet:
""" Documentation window """

import wx
import logging
from c.ui.wxw.widgets.baseframe import BaseFrame
from c.ui.wxw.editor import Editor
from c.compiler.spy import SpyClass, SpyMethod

class DocumentationWindow(BaseFrame):
	def __init__(self, parent):
		self.parent = parent

		BaseFrame.__init__(self, parent, "Documentation")
		
		self.Bind(wx.EVT_CLOSE, self.onClose)
		self.item = None
		self.anyChange = False

		self.panel = wx.Panel(self, -1)
		
		self.splitter = wx.SplitterWindow(self.panel, -1, style=wx.SP_LIVE_UPDATE)
		self.panelExample = wx.Panel(self.splitter, -1)
		self.panelDescr = wx.Panel(self.splitter, -1)

		self.lblHdrTopA = wx.StaticText(self.panel, -1, "")
		self.lblTopA = wx.StaticText(self.panel, -1, "")
		self.lblHdrTopB = wx.StaticText(self.panel, -1, "")
		self.lblTopB = wx.StaticText(self.panel, -1, "")
		self.static_line_1 = wx.StaticLine(self.panel, -1)
		self.lblHdrDescr = wx.StaticText(self.panelDescr, -1, "Description:")
		self.lblByteInfo = wx.StaticText(self.panelDescr, -1, "")
		self.editorDesc = Editor(self.panelDescr)
		self.editorDesc.disableSyntax()
		self.lblHdrExample = wx.StaticText(self.panelExample, -1, "Examples:")
		self.lblLitInfo = wx.StaticText(self.panelExample, -1, "")
		self.editorExample = Editor(self.panelExample)

		self.__set_properties()
		self.__do_layout()
		# end wxGlade

	def __set_properties(self):
		# begin wxGlade: GetInfoMethodDialog.__set_properties
		ft = self.lblTopA.GetFont()
		newft = wx.Font(ft.GetPointSize(), wx.DECORATIVE, wx.NORMAL, wx.BOLD, 0, ft.GetFaceName())

		self.lblHdrTopA.SetFont(newft)
		self.lblHdrTopB.SetFont(newft)
		self.lblHdrDescr.SetFont(newft)
		self.lblHdrExample.SetFont(newft)
		# end wxGlade

	def __do_layout(self):
		# begin wxGlade: GetInfoMethodDialog.__do_layout
		self.mainSizer = wx.BoxSizer(wx.VERTICAL)
		sizer_8 = wx.BoxSizer(wx.VERTICAL)
		sizer_3 = wx.BoxSizer(wx.HORIZONTAL)
		sizer_7 = wx.BoxSizer(wx.VERTICAL)
		sizer_2 = wx.BoxSizer(wx.HORIZONTAL)
		grid_sizer_1 = wx.FlexGridSizer(2, 2, 5, 5)
		grid_sizer_1.Add(self.lblHdrTopA, 0, wx.ALIGN_RIGHT, 0)
		grid_sizer_1.Add(self.lblTopA, 0, wx.EXPAND, 0)
		grid_sizer_1.Add(self.lblHdrTopB, 0, wx.ALIGN_RIGHT, 0)
		grid_sizer_1.Add(self.lblTopB, 0, wx.EXPAND, 0)
		self.mainSizer.Add(grid_sizer_1, 0, wx.ALL|wx.EXPAND, 5)
		self.mainSizer.Add(self.static_line_1, 0, wx.EXPAND, 0)
		sizer_2.Add(self.lblHdrDescr, 0, wx.TOP|wx.BOTTOM, 5)
		sizer_2.Add(self.lblByteInfo, 0, wx.LEFT|wx.TOP|wx.BOTTOM, 5)
		sizer_7.Add(sizer_2, 0, wx.EXPAND, 0)
		sizer_7.Add(self.editorDesc, 1, wx.BOTTOM|wx.EXPAND, 5)
		self.panelDescr.SetSizer(sizer_7)
		sizer_3.Add(self.lblHdrExample, 0, wx.TOP|wx.BOTTOM, 5)
		sizer_3.Add(self.lblLitInfo, 0, wx.LEFT|wx.TOP|wx.BOTTOM, 5)
		sizer_8.Add(sizer_3, 0, wx.EXPAND, 0)
		sizer_8.Add(self.editorExample, 1, wx.EXPAND, 0)
		self.panelExample.SetSizer(sizer_8)
		self.splitter.SplitHorizontally(self.panelDescr, self.panelExample)
		self.mainSizer.Add(self.splitter, 1, wx.LEFT|wx.RIGHT|wx.BOTTOM|wx.EXPAND, 5)
		self.panel.SetSizer(self.mainSizer)
		self.mainSizer.Fit(self)
		self.Layout()

	def onClose(self, event):
		self.Show(False)

	def hasChanged(self):
		if (self.item and (self.editorDesc.hasChanged() or 
						   self.editorExample.hasChanged())):
			self.item.docs = "Info:\n%s\nExamples:\n%s\n" % (
					self.editorDesc.GetText().rstrip(" \n"),
					self.editorExample.GetText().rstrip(" \n"))
			self.anyChange = True
			return True
		if self.anyChange:
			return True
		return False

	def setItem(self, item):
		self.hasChanged()
		self.item = item
		if isinstance(item, SpyMethod) or isinstance(item, SpyClass):
			d = item.docs
			self.editorDesc.Enable(True)
			self.editorExample.Enable(True)
			if "Info:" in d and "Examples:" in d:
				idxInfo = d.index("Info:")
				idxExample = d.index("Examples:")
				self.editorDesc.setText(d[idxInfo+6:idxExample].rstrip(" \n"))
				self.editorExample.setText(d[idxExample+10:].rstrip(" \n"))
			else:
				self.editorDesc.setText(d.rstrip(" \n"))
				self.editorExample.setText("")
			self.lblHdrTopA.SetLabel("Name:")
			self.lblTopA.SetLabel(item.name)
			if isinstance(item, SpyMethod):
				self.lblHdrTopB.SetLabel("In Class:")
				self.lblTopB.SetLabel(item.incls.name)
			elif isinstance(item, SpyClass):
				self.lblHdrTopB.SetLabel("Superclass:")
				if hasattr(item.parent,"name"):
					self.lblTopB.SetLabel(item.parent.name)
				else:
					self.lblTopB.SetLabel("nil")
		else:
			self.lblTopA.SetLabel("")
			self.lblTopB.SetLabel("")
			self.editorDesc.setText("")
			self.editorExample.setText("")
			self.editorDesc.Enable(False)
			self.editorExample.Enable(False)
		self.panel.Layout()
		self.splitter.SetSashPosition(-100)

